let app = getApp();
const { domain } = app.siteInfo;
Component({
  properties: {
    isShowClosure: {
      type: Boolean,
      value: false,
      observer: function(newVal, oldVal) {
        this.setData({ isClosure: newVal });
      },
    },
    scanQueryStr: {
      type: String,
      value: '',
      observer: function(newVal, oldVal) {
        this.setData({ q: newVal });
      },
    },
    lng: {
      type: String,
      value: '',
    },
    lat: {
      type: String,
      value: '',
    },
  },
  data: {
    bounceIn: '',
    shake: '',
    isFireUse: false,
    winning: false,
    nothing: false,
    domain: domain,
    fireGift: false,
  },
  methods: {
    _fireClose(e) {
      let { opertype } = e.currentTarget.dataset;
      if (Number(opertype) === 1) {
        wx.showToast({
          title: '领取成功！',
          icon: 'success',
          duration: 1500,
          mask: true,
        });
      }
      this.setData({
        isShowClosure: false,
      });
    },
    _fireLottery() {
      let _this = this;
      let { q, lng, lat } = _this.data;
      wx.showNavigationBarLoading();
      if (_this.data.fireGift) {
        return;
      }
      
      _this.setData({
        shake: 'shake',
        fireGift: true,
      });
      setTimeout(function() {
        app.util.request({
          url: 'flow/api/scan',
          cachetime: 0,
          data: {
            q,
            lng,
            lat,
          },
          success: function(res) {
            wx.hideLoading();
            if (Number(res.data.errno) === 0) {
              if (res.data) {
                _this.setData({
                  winning: true,
                  isFireUse: true,
                  shake: '',
                  sub_price: parseInt(res.data.data.sub_price, 10),
                  min_price: parseInt(res.data.data.min_price, 10),
                  type: res.data.data.type,
                });
              } else {
                _this.setData({
                  _fireClose: '_fireClose',
                  nothing: true,
                  isFireUse: true,
                  shake: '',
                });
              }
            } else {
              _this.setData({
                isShowClosure: false,
                shake: '',
              });
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 1500,
                mask: true,
              });
            }
          },
          fail: function(res) {
            _this.setData({
              isShowClosure: false,
            });
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1500,
              mask: true,
            });
          },
        });
      }, 2000);
    },
  },
  ready() {
    let _this = this;
    _this.setData({
      bounceIn: 'bounceIn',
    });
  },
});
